package com.KuoHaoPiPei;

public class SequenceStack<T> {
    final int MaxSize=10;
    private T[]stackArray;
    private int top;

    public SequenceStack() {
        top=-1;
        stackArray= (T[]) new Object[MaxSize];
    }

    public SequenceStack(int n) {
       if (n<=0){
           System.out.println("数组长度要大于零");
           System.exit(1);
       }
       top=-1;
       stackArray= (T[]) new Object[n];
    }
    public void push(T obj){
        if (top==stackArray.length-1){
            T[]temp= (T[]) new Object[top*2+2];
            for (int i = 0; i < stackArray.length-1; i++) {
                temp[i]=stackArray[i];
            }
            stackArray=temp;
        }
        top++;
        stackArray[top]=obj;
    }
    public T pop(){
        if (top==-1){
            System.out.println("栈为空栈");
            return null;
        }
        top--;
        return stackArray[top+1];
    }
    //判断当前栈是否为空
    public boolean isEmpty(){
        return top==-1;
    }
    public int size(){
        return top+1;
    }
    //依次访问栈中每个元素，并输出
    public void nextOrder(){
        for (int i=top;i>=0;i--){
            System.out.println(stackArray[i]+"\t");
        }
        System.out.println("\n");

    }
    //销毁栈
    public void clear(){
        top=-1;
    }
    public T getHead(){
        if (top>=0){
            return stackArray[top];
        }
       return null;
    }


}
